package com.dreamwhale.machinery.pay.ali.controller;

import com.dreamwhale.machinery.config.DataResult;

import com.dreamwhale.machinery.pay.ali.server.AliPayService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.function.EntityResponse;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

@RestController
@RequestMapping("aliPay")
public class AliPayController {


    @Resource
    private AliPayService aliPayService;

    @RequestMapping("recharge")
    public ResponseEntity<DataResult> recharge(BigDecimal money, HttpServletRequest request) {
        String userId = request.getAttribute("userId").toString();
        return ResponseEntity.ok(aliPayService.recharge(money, Integer.parseInt(userId)));
    }

    @RequestMapping("order")
    public ResponseEntity<DataResult> order(@RequestBody String orderId, HttpServletRequest request) {
        String userId = "0";
        if (request.getAttribute("userId") != null) {
            userId = request.getAttribute("userId").toString();
        }
        return ResponseEntity.ok(aliPayService.order(orderId, Integer.parseInt(userId)));
    }

    @RequestMapping("notify")
    public String notify(HttpServletRequest request, HttpServletResponse response) {
        return aliPayService.notify(request, response);
    }

    @RequestMapping("refund")
    public ResponseEntity<DataResult> refund(String orderId, String reason) {
        return ResponseEntity.ok(aliPayService.refund(orderId, reason));
    }
}
